package com.mocoop.jfqc;


import com.mocoop.activity.MocoopActivity;
import com.mocoop.commom.MyConstants;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

public class LaunchActivity extends Activity {

    private EditText baseServiceURLEditText,baseHTMLURLEditText;
    private LinearLayout configLayout;
    private Button saveConfigBut;
    private String  baseServiceURL,baseHTMLURL;
    private CheckBox isSingleton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lauch);
      
        configLayout = (LinearLayout)findViewById(R.id.config_layout);
        baseServiceURLEditText = (EditText) findViewById(R.id.base_service_url_edit_text);
        baseHTMLURLEditText = (EditText)findViewById(R.id.base_html_url_edit_text);
        saveConfigBut = (Button)findViewById(R.id.save_config);
        isSingleton = (CheckBox)findViewById(R.id.isSingleton);
        
        getConfig();
        saveConfigBut.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View arg0) {
				setConfig();
			}
        });
    }

    private void getConfig(){
    	
    	SharedPreferences sharedPreferences = getSharedPreferences("config", 0);
	     baseServiceURL = sharedPreferences.getString("BASE_SERVICE_URL", MyConstants.SERVICE_URL);
	     baseHTMLURL = sharedPreferences.getString("BASE_HTML_URL", MocoopActivity.getBASE_URL());
	     isSingleton.setChecked(sharedPreferences.getBoolean("SINGLETON",isSingleton.isChecked()));
	     baseServiceURLEditText.setText(baseServiceURL);
	     baseHTMLURLEditText.setText(baseHTMLURL);

    }
    
    
    private void setConfig(){
    	baseHTMLURL = baseHTMLURLEditText.getText().toString();
    	baseServiceURL = baseServiceURLEditText.getText().toString();
    	LoginActivity.isSingleton  = isSingleton.isChecked();
    	configLayout.setVisibility(View.GONE);
    	SharedPreferences sharedPreferences = getSharedPreferences("config", 0);   
	     MyConstants.SERVICE_URL = baseServiceURL;
	     MocoopActivity.setBASE_URL(baseHTMLURL);
	     Editor editor = sharedPreferences.edit(); 
		 editor.putString("BASE_SERVICE_URL", baseServiceURL);
		 editor.putString("BASE_HTML_URL", baseHTMLURL);
		 editor.putBoolean("SINGLETON",isSingleton.isChecked());
		 editor.commit();
		 skip();
    }
    
    
    

    private void skip() {
        finish();
    }
}
